class FieldController {
    constructor() {
        Laya.timer.loop(Cfg.LoopTime, this, this.OnLoop);
    }

    OnLoop() : void {
        //检测所有农作物的生长状态
        if (Game.serverTime <= 0) {
            return;
        }
        let fieldList = FieldModel.GetFieldList();
        if (fieldList) {
            let stageList = FieldModel.GetStageList();
            for (let i = 0; i < fieldList.length; i++) {
                let fieldData = fieldList[i];
                if (null != fieldData.cropId) {
                    let effectTime = 0; //道具施加在作物上影响的时间
                    fieldData.propList.forEach(element => {
                        if( Cfg.ItemCfg["prop_effect_time"]["value"]==Cfg.ItemCfg[element.itemId]["effect_type"])
                            effectTime += CtrlManager.GetCtrl(CtrlNames.Warehouse)[Cfg.ItemCfg[Cfg.ItemCfg[element.itemId]["effect_supply"]]["value"]](element.itemId) * element.number;
                    });
                    //计算农作物当前阶段
                    let stageTime = Math.floor(Cfg.ItemCfg[fieldData.cropId].time / Cfg.ItemCfg[fieldData.cropId].stage);
                    let curStage = Math.floor((Game.serverTime - fieldData.plantTime + effectTime) / stageTime) + 1;
                    if (curStage <= 0) {
                        continue;
                    }
                    if (curStage > Cfg.ItemCfg[fieldData.cropId].stage+1) {
                        curStage = Cfg.ItemCfg[fieldData.cropId].stage+1;
                    }
                    if (curStage != stageList[fieldData.index]) {
                        FieldModel.ChangeState(fieldData.index, curStage);
                        Game.viewFarm.ChangeState(fieldData.index, curStage);
                    }
                }
            }
        }
    }

    SetField() : void {
        //更新玩家地块数据
        Game.viewFarm.SetField();
    }

    //种植(背包模块调用)
    Plant(cropId:number) : Boolean {
        let index = FieldModel.GetSelected();
        let fieldData = FieldModel.GetFieldData(index);
        if (index <= 0 || null != fieldData) {
            index = FieldModel.GetFreeField();
        }
        if (index <= 0) {
            return false;
        }
        ComFun.BaseCall("plant", index, cropId);
        return true;
    }

    //种植(服务端返回)
    OnPlant(index:number, cropId:number) : void {
        FieldModel.Plant(index);
        Game.viewFarm.Plant(index, cropId);
        //收到种植成功时，改变种子数量
        if (PlayerModel.IsSelfFarm()) {
            CtrlManager.GetCtrl(CtrlNames.Warehouse).ChangeSeedData(cropId, -1);
        }
    }

    //点击气泡
    OnBubbleClick(index:number) : void {
        let fieldData = FieldModel.GetFieldData(index);
        if (null != fieldData) {
            let curStage = FieldModel.getStage(index);
            if (curStage > Cfg.ItemCfg[fieldData.cropId].stage) {
                if (PlayerModel.IsSelfFarm()) {
                    //本人农场 - 收获
                    ComFun.BaseCall("harvest", index);
                } else {
                    //好友农场 - 偷菜
                    ComFun.BaseCall("steal", PlayerModel.GetFriendDbid(), index);
                }
            }
            else if (fieldData.disasterId > 1) {
                //浇水、除草、杀虫
                if (PlayerModel.IsSelfFarm()) {
                    //本人农场
                    ComFun.BaseCall("clearDisaster", index);
                } else {
                    //好友农场
                    ComFun.BaseCall("help", PlayerModel.GetFriendDbid(), index);
                }
            }
        }
    }

    //收获(服务端返回)
    OnHarvest(index:number) : void {
        FieldModel.Harvest(index);
        Game.viewFarm.Harvest(index);
    }

    //扩建(服务端返回)
    OnExpand(fieldNum:number) : void {
        Game.viewFarm.SetFieldNum(fieldNum);
    }

    //设置天气
    SetWeather() {
        Game.viewFarm.SetWeather();
    }

    //农作物获得灾害(服务端通知)
    AddDisaster(weatherId:number, fieldIndexArray:any) {
        let disasterId = Cfg.WeatherCfg[weatherId].disaster
        fieldIndexArray.forEach(index => {
            FieldModel.SetDisaster(index, disasterId);
        });
        Game.viewFarm.AddDisaster(fieldIndexArray);
    }

    //清除所有农作物灾害
    ClearAllDisaster() {
        Game.viewFarm.ClearAllDisaster();
    }

    //清除指定农作物的灾害
    OnClearDisaster(index:number) {
        Game.viewFarm.OnClearDisaster(index);
    }

    //选中空地块
    SetSelected(index:number) {
        //设置当前选中地块索引
        FieldModel.SetSelected(index);
        //显示背包
        CtrlManager.GetCtrl(CtrlNames.Warehouse).SetSeedData();
		CtrlManager.GetCtrl(CtrlNames.Warehouse).ShowDlg();
    }

    //清除选中状态
    ClearSelected() {
        Game.viewFarm.ClearSelected();
        FieldModel.ClearSelected();
    }
}